﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Serialization;

namespace PFWFA
{
    public partial class Browser1 : Form
    {
        String currentURL;
        //StringCollection savedHistoryList;
        //ArrayList bookmarklist = new ArrayList();

        public Browser1()
        {
            InitializeComponent();
            wb1.Navigate(Properties.Settings.Default.homePage);

            //Populate bookmarks list from saved list.
            foreach(String s in Properties.Settings.Default.savedBookmarkList)
            {
                cbBookmarks.Items.Add(s);
            }
            //Populate history list from saved list.
            //lbHistory = new ListBox();
            foreach(String s in Properties.Settings.Default.savedHistoryList)
            {
                lbHistory.Items.Insert(0, s);
            }
        }

        private void btnGoURL_Click(object sender, EventArgs e)
        {
            wb1.Navigate(boxURL.Text);
        }

        private void wb1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
        {
            if ( !(wb1.Url.ToString().Equals("about:blank")) && !(wb1.Url.ToString().Equals(currentURL)) )
            {
                lblTitle.Text = wb1.DocumentTitle;
                currentURL = wb1.Url.ToString();
                boxURL.Text = currentURL;

                lbHistory.Items.Insert(0, currentURL);

                Properties.Settings.Default.savedHistoryList.Add(currentURL);
            }
            
            
            Properties.Settings.Default.Save();
        }

        private void btnForward_Click(object sender, EventArgs e)
        {
            wb1.GoForward();
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            wb1.GoBack();
        }

        private void wb1_NewWindow(object sender, CancelEventArgs e)
        {
            e.Cancel = true;
            HtmlElement currentElement = wb1.Document.ActiveElement;
            string currentURL = currentElement.GetAttribute("href");
            wb1.Navigate(currentURL);
            //currentURL = targetPath;
            //boxURL.Text = currentURL;
        }

        private void btnHome_Click(object sender, EventArgs e)
        {
            currentURL = Convert.ToString(Properties.Settings.Default["homePage"]);
            wb1.Navigate(currentURL);
        }

        private void boxURL_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyCode == Keys.Return)
            {
                wb1.Navigate(boxURL.Text);
            }
        }

        private void cbBookmarks_SelectedIndexChanged(object sender, EventArgs e)
        {
            wb1.Navigate(cbBookmarks.SelectedItem.ToString());
        }

        private void btnSetHome_Click(object sender, EventArgs e)
        {
            Properties.Settings.Default["homePage"] = currentURL;
            Properties.Settings.Default.Save();
        }

        private void btnBookmark_Click(object sender, EventArgs e)
        {
            Properties.Settings.Default.savedBookmarkList.Add(currentURL);
            Properties.Settings.Default.Save();
            cbBookmarks.Items.Add(currentURL);
        }

        private void cbBookmarks_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyCode == Keys.Delete && cbBookmarks.SelectedItem != null)
            {
                Properties.Settings.Default.savedBookmarkList.Remove(cbBookmarks.SelectedItem.ToString());
                cbBookmarks.Items.Remove(cbBookmarks.SelectedItem.ToString());
                
                Properties.Settings.Default.Save();
            }
            /*
            if(e.KeyCode == Keys.Delete)
            {
                if(cbBookmarks.SelectedItem != null)
                { 
                if (cbBookmarks.Items.Count == 1)
                {
                    cbBookmarks = new ComboBox();
                    Properties.Settings.Default.savedBookmarkList = new StringCollection();
                }
                else
                {
                    cbBookmarks.Items.Remove(cbBookmarks.SelectedItem.ToString());
                    try
                    {
                        Properties.Settings.Default.savedBookmarkList.Remove(cbBookmarks.SelectedItem.ToString());
                    }
                    catch (System.NullReferenceException ex)
                    {
                        
                    }
                }
                }
            }
                
                Properties.Settings.Default.Save();
            }*/
        }
          

        private void btnClearHistory_Click(object sender, EventArgs e)
        {
            lbHistory.Items.Clear();
            Properties.Settings.Default.savedHistoryList = new StringCollection();
            Properties.Settings.Default.Save();
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            wb1.Navigate(currentURL);
        }
                
}
}
